package widget;

import android.content.Context;
import android.support.annotation.NonNull;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.FrameLayout;
import android.widget.ListView;
import android.widget.TextView;
import android.widget.Toast;

import net.ck.hzq.R;

import java.util.ArrayList;
import java.util.List;

import Bean.AddressBean;
import Utils.HelloWordModel;
import Utils.SystemDatas;
import retrofit2.Call;
import retrofit2.Response;

/**
 * 城市选择view
 * Created by zhangdong on 2017/11/26.
 */

public class CitySelectView extends FrameLayout {

    private OnAddressSelectedListener mListener;

    private ListView newaddress_list1, newaddress_list2, newaddress_list3;

    ListviewCityAdapter1 listviewCityAdapter1;
    ListviewCityAdapter2 listviewCityAdapter2;
    ListviewCityAdapter3 listviewCityAdapter3;

    private AddressBean addressBean;

    private int posi = 0;
    private String code1, code2, code3;
    private String address_s, address_c, address_q;

    public CitySelectView(Context context) {
        this(context, null);
    }

    public CitySelectView(Context context, AttributeSet attrs) {
        super(context, attrs);
        initView();
    }

    private void initView() {
        LayoutInflater.from(getContext()).inflate(R.layout.view_city_select, this);

        newaddress_list1 = findViewById(R.id.newaddress_list1);
        newaddress_list2 = findViewById(R.id.newaddress_list2);
        newaddress_list3 = findViewById(R.id.newaddress_list3);

        findViewById(R.id.wrap_bg).setOnClickListener(v -> setVisibility(View.GONE));

        Call<AddressBean> userMsgCall = HelloWordModel.getInstance(getContext()).getCity(SystemDatas.GetService_URL
                ("getcity"));
        userMsgCall.enqueue(new retrofit2.Callback<AddressBean>() {
            @Override
            public void onResponse(@NonNull Call<AddressBean> call, @NonNull final Response<AddressBean> response) {
              try{
                AddressBean body = response.body();
                if (body != null && body.getMsg().equals("success")) {
                    addressBean = body;

                    List<AddressBean.DataBean.KBean> provinces = addressBean.getData().getK();
                    if (provinces == null || provinces.isEmpty()) {
                        return;
                    }
                    AddressBean.DataBean.KBean firstProvince = provinces.get(0);
                    if (firstProvince == null) {
                        return;
                    }
                    List<AddressBean.DataBean.KBean.ChildrenBeanX> cities = firstProvince.getChildren();
                    if (cities == null || cities.isEmpty() || cities.get(0) == null) {
                        return;
                    }
                    AddressBean.DataBean.KBean.ChildrenBeanX firstCity = cities.get(0);
                    List<AddressBean.DataBean.KBean.ChildrenBeanX.ChildrenBean> districts = firstCity.getChildren();
                    if (districts == null || districts.isEmpty() || districts.get(0) == null) {
                        return;
                    }
                    AddressBean.DataBean.KBean.ChildrenBeanX.ChildrenBean firstDistrict = districts.get(0);

                    listviewCityAdapter1 = new ListviewCityAdapter1(provinces, getContext());
                    newaddress_list1.setAdapter(listviewCityAdapter1);
                    listviewCityAdapter2 = new ListviewCityAdapter2(cities, getContext());
                    newaddress_list2.setAdapter(listviewCityAdapter2);
                    listviewCityAdapter3 = new ListviewCityAdapter3(districts, getContext());
                    newaddress_list3.setAdapter(listviewCityAdapter3);

                    code1 = firstProvince.getValue();
                    code2 = firstCity.getValue();
                    code3 = firstDistrict.getValue();
                    address_s = firstProvince.getText();
                    address_c = firstCity.getText();
                    address_q = firstDistrict.getText();
                }
              } catch (Exception e) {
                  e.printStackTrace();
              }
              }

            @Override
            public void onFailure(Call<AddressBean> call, Throwable t) {
                Toast.makeText(getContext(), "城市获取失败", Toast.LENGTH_SHORT).show();
            }
        });

    }

    public void setAddressSelectedListener(OnAddressSelectedListener listener) {
        mListener = listener;
    }

    private class ListviewCityAdapter1 extends BaseAdapter {

        private List<AddressBean.DataBean.KBean> data;//数据

        private Context context;//上下文

        public ListviewCityAdapter1(List<AddressBean.DataBean.KBean> data, Context context) {
            this.data = data;
            this.context = context;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
            ViewHolder viewHolder;
            if (convertView == null) {
                view = LayoutInflater.from(context).inflate(R.layout.listviewcity_item, null, false);
                viewHolder = new ViewHolder();
                viewHolder.textView1 = view.findViewById(R.id.listviewcity_txt);
                view.setTag(viewHolder);
            } else {
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
            }

            viewHolder.textView1.setText(data.get(position).getText());

            viewHolder.textView1.setOnClickListener(v -> {
                posi = position;
                try {
                    AddressBean.DataBean.KBean selectedProvince = data.get(position);
                    if (selectedProvince == null) {
                        return;
                    }
                    List<AddressBean.DataBean.KBean.ChildrenBeanX> cities = selectedProvince.getChildren();
                    if (cities == null || cities.isEmpty() || cities.get(0) == null) {
                        emptyCities();
                        emptyDistricts();
                        if (mListener != null) {
                            mListener.onAddressSelected(selectedProvince.getValue(), selectedProvince.getText());
                        }
                        return;
                    }
                    AddressBean.DataBean.KBean.ChildrenBeanX firstCity = cities.get(0);
                    List<AddressBean.DataBean.KBean.ChildrenBeanX.ChildrenBean> districts = firstCity.getChildren();
                    if (districts == null || districts.isEmpty() || districts.get(0) == null) {
                        return;
                    }
                    AddressBean.DataBean.KBean.ChildrenBeanX.ChildrenBean firstDistrict = districts.get(0);

                    code1 = data.get(position).getValue();
                    code2 = data.get(position).getChildren().get(0).getValue();
                    code3 = data.get(position).getChildren().get(0).getChildren().get(0).getValue();
                    address_s = data.get(position).getText();
                    address_c = data.get(position).getChildren().get(0).getText();
                    address_q = data.get(position).getChildren().get(0).getChildren().get(0).getText();
                    listviewCityAdapter2 = new ListviewCityAdapter2(
                            addressBean.getData().getK().get(position).getChildren(), getContext());
                    newaddress_list2.setAdapter(listviewCityAdapter2);
                    listviewCityAdapter2.notifyDataSetChanged();
                    if (addressBean.getData().getK().get(position).getChildren().size() > 0) {
                        listviewCityAdapter3 = new ListviewCityAdapter3(addressBean.getData().getK().get(position)
                                .getChildren().get(0).getChildren(), getContext());
                        newaddress_list3.setAdapter(listviewCityAdapter3);
                        listviewCityAdapter3.notifyDataSetChanged();
                    }
                } catch (Exception e) {

                }
            });

            return view;
        }

        private class ViewHolder {
            TextView textView1;
        }

    }

    private void emptyCities() {
        listviewCityAdapter2 = new ListviewCityAdapter2(new ArrayList<>(), getContext());
        newaddress_list2.setAdapter(listviewCityAdapter2);
        listviewCityAdapter2.notifyDataSetChanged();
    }

    private void emptyDistricts() {
        listviewCityAdapter3 = new ListviewCityAdapter3(new ArrayList<>(), getContext());
        newaddress_list3.setAdapter(listviewCityAdapter3);
        listviewCityAdapter3.notifyDataSetChanged();
    }

    private class ListviewCityAdapter2 extends BaseAdapter {

        private List<AddressBean.DataBean.KBean.ChildrenBeanX> data;//数据

        private Context context;//上下文

        public ListviewCityAdapter2(List<AddressBean.DataBean.KBean.ChildrenBeanX> data, Context context) {
            this.data = data;
            this.context = context;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
            ViewHolder viewHolder;
            if (convertView == null) {
                view = LayoutInflater.from(context).inflate(R.layout.listviewcity_item, null, false);
                viewHolder = new ViewHolder();
                viewHolder.textView1 = (TextView) view.findViewById(R.id.listviewcity_txt);
                view.setTag(viewHolder);
            } else {
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
            }

            viewHolder.textView1.setText(data.get(position).getText());

            viewHolder.textView1.setOnClickListener(v -> {
                try {
                    code2 = data.get(position).getValue();
                    code3 = data.get(position).getChildren().get(0).getValue();
                    address_c = data.get(position).getText();
                    address_q = data.get(position).getChildren().get(0).getText();
                    if (addressBean.getData().getK().get(posi).getChildren().get(position).getChildren().size() > 0) {
                        listviewCityAdapter3 = new ListviewCityAdapter3(addressBean.getData().getK().get(posi).getChildren
                                ().get(position).getChildren(), getContext());

                    } else {
                        listviewCityAdapter3 = new ListviewCityAdapter3(new ArrayList<>(), getContext());
                    }
                    newaddress_list3.setAdapter(listviewCityAdapter3);
                    listviewCityAdapter3.notifyDataSetChanged();
                } catch (Exception e) {

                }
            });

            return view;
        }

        private class ViewHolder {
            TextView textView1;
        }

    }

    private class ListviewCityAdapter3 extends BaseAdapter {

        private List<AddressBean.DataBean.KBean.ChildrenBeanX.ChildrenBean> data;//数据

        private Context context;//上下文

        public ListviewCityAdapter3(List<AddressBean.DataBean.KBean.ChildrenBeanX.ChildrenBean> data, Context context) {
            this.data = data;
            this.context = context;
        }

        @Override
        public int getCount() {
            return data.size();
        }

        @Override
        public Object getItem(int position) {
            return data.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, ViewGroup parent) {
            View view;
            ViewHolder viewHolder;
            if (convertView == null) {
                view = LayoutInflater.from(context).inflate(R.layout.listviewcity_item, null, false);
                viewHolder = new ViewHolder();
                viewHolder.textView1 = view.findViewById(R.id.listviewcity_txt);
                view.setTag(viewHolder);
            } else {
                view = convertView;
                viewHolder = (ViewHolder) view.getTag();
            }
            viewHolder.textView1.setText(data.get(position).getText());

            viewHolder.textView1.setOnClickListener(v -> {
                try {
                    code3 = data.get(position).getValue();
                    address_q = data.get(position).getText();
                } catch (Exception e) {

                }
                String resultAddress;
                if (address_q != null) {
                    resultAddress = address_s + "-" + address_c + "-" + address_q;
//                        sm_edit_dq.setText(address_s + "-" + address_c + "-" + address_q);
//                        Toast.makeText(SmActivity.this, address_s + address_c + address_q, Toast.LENGTH_SHORT).show();
                } else if (address_c != null) {
                    resultAddress = address_s + "-" + address_c;
//                        sm_edit_dq.setText(address_s + "-" + address_c);
//                        Toast.makeText(SmActivity.this, address_s + address_c, Toast.LENGTH_SHORT).show();
                } else {
                    resultAddress = address_s;
//                        sm_edit_dq.setText(address_s);
//                        Toast.makeText(SmActivity.this, address_s, Toast.LENGTH_SHORT).show();
                }
                if (mListener != null) {
                    mListener.onAddressSelected(code1 + " " + code2 + " " + code3, resultAddress);
                }
            });

            return view;
        }

        private class ViewHolder {
            TextView textView1;
        }

    }

    public interface OnAddressSelectedListener {
        void onAddressSelected(String code, String address);
    }

}
